package com.houseliker.activity.calculate;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.houseliker.R;
import com.houseliker.activity.BaseActivity;
import com.houseliker.adapter.calculate.LoanListAdapter;
import com.houseliker.model.LoanDetail;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 组合贷款详情页
 * Created by sunrx on 2015/12/24.
 */
public class ZHLoanDetailActivity extends BaseActivity {

    private TextView tv_amountloan;
    private TextView tv_interest;
    private TextView tv_payment;
    private TextView tv_monthcount;
    private ListView lv_loandetail;

    //公积金-贷款金额
    private double gjj_loanAmount;
    //商业-贷款金额
    private double sy_loanAmount;
    //贷款月数
    private int gjj_monthCount;
    //贷款月数
    private int sy_monthCount;
    //贷款金额
    private double loanAmount;
    //总利息
    private double interestAmount;
    //月本息集合
    private double[] gjj_monthlyPrincipalInterests;
    //月本息集合
    private double[] sy_monthlyPrincipalInterests;
    //月均本金
    private double monthlyPrincipal;
    //月均利息
    private double monthlyInterest;
    //公积金-月利率
    private double gjj_monthlyRate;
    //商业-月利率
    private double sy_monthlyRate;

    //贷款类型:等额本息(0)/等额本金(1)
    private int type;

    private List<LoanDetail> lists;

    private DecimalFormat df = new DecimalFormat("0");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.my_loandetail, TYPE_HEADER);
        setHeaderBar("还款详情");

        fetchIntents();

        initViews();

        fillDatas();
    }

    private void fetchIntents() {
        gjj_loanAmount = getIntent().getDoubleExtra("gjj_loanAmount", 0);
        sy_loanAmount = getIntent().getDoubleExtra("sy_loanAmount", 0);
        interestAmount = getIntent().getDoubleExtra("interestAmount", 0);
        gjj_monthCount = getIntent().getIntExtra("gjj_monthCount", 0);
        sy_monthCount = getIntent().getIntExtra("sy_monthCount", 0);
        gjj_monthlyPrincipalInterests = getIntent().getDoubleArrayExtra("gjj_monthlyPrincipalInterests");
        sy_monthlyPrincipalInterests = getIntent().getDoubleArrayExtra("sy_monthlyPrincipalInterests");
        gjj_monthlyRate = getIntent().getDoubleExtra("gjj_monthlyRate", 0);
        sy_monthlyRate = getIntent().getDoubleExtra("sy_monthlyRate", 0);
        type = getIntent().getIntExtra("type", 0);
    }

    private void initViews() {
        tv_amountloan = (TextView) findViewById(R.id.tv_amountloan);
        tv_interest = (TextView) findViewById(R.id.tv_interest);
        tv_payment = (TextView) findViewById(R.id.tv_payment);
        tv_monthcount = (TextView) findViewById(R.id.tv_monthcount);
        lv_loandetail = (ListView) findViewById(R.id.lv_loandetail);
    }

    private void fillDatas() {
        int monthCount;
        if (gjj_monthCount > sy_monthCount) {
            monthCount = gjj_monthCount;
        } else {
            monthCount = sy_monthCount;
        }
        loanAmount = gjj_loanAmount + sy_loanAmount;
        tv_amountloan.setText("贷款总额：" + df.format(loanAmount / 10000) + "万");
        tv_interest.setText("支付利息：" + df.format(interestAmount / 10000) + "万");
        tv_payment.setText("月均还款：" + df.format((loanAmount + interestAmount) / monthCount) + "元");
        tv_monthcount.setText("贷款月数：" + monthCount + "月(" + monthCount / 12 + "年)");
        //
        lists = new ArrayList<>();

        double sum_monthlyPrincipalInterests = 0;
        double gjj_remainingLoanAmount = gjj_loanAmount;
        double sy_remainingLoanAmount = sy_loanAmount;
        if (gjj_monthCount > sy_monthCount) {
            int size = sy_monthlyPrincipalInterests.length;
            for (int i = 0; i < size; i++) {
                if (type == 1) {
                    //等额本金
                    monthlyPrincipal = gjj_loanAmount / gjj_monthCount + sy_loanAmount / sy_monthCount;
                    monthlyInterest = gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i] - monthlyPrincipal;
                } else {
                    //等额本息
                    monthlyInterest = gjj_remainingLoanAmount * gjj_monthlyRate + sy_remainingLoanAmount * sy_monthlyRate;
                    monthlyPrincipal = gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i] - monthlyInterest;
                    gjj_remainingLoanAmount -= (gjj_monthlyPrincipalInterests[i] - gjj_remainingLoanAmount * gjj_monthlyRate);
                    sy_remainingLoanAmount -= (sy_monthlyPrincipalInterests[i] - sy_remainingLoanAmount * sy_monthlyRate);
                }
                sum_monthlyPrincipalInterests += gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i];
                double surplus = loanAmount + interestAmount - sum_monthlyPrincipalInterests;
                //
                LoanDetail detail = new LoanDetail();
                detail.year = "第" + (i / 12 + 1) + "年";
                detail.month = (i % 12 + 1) + "";
                detail.payment = df.format(gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i]) + "";
                detail.payment_principal = df.format(monthlyPrincipal) + "";
                detail.payment_interest = df.format(monthlyInterest) + "";
                detail.surplus = df.format(surplus) + "";
                detail.state = (i % 12) % 2;
                //
                lists.add(detail);
            }
            if (gjj_monthCount - sy_monthCount > 0) {
                int _size = gjj_monthCount - sy_monthCount + size;
                for (int i = size; i < _size; i++) {
                    if (type == 1) {
                        //等额本金
                        monthlyPrincipal = gjj_loanAmount / gjj_monthCount;
                        monthlyInterest = gjj_monthlyPrincipalInterests[i] - monthlyPrincipal;
                    } else {
                        //等额本息
                        monthlyInterest = gjj_remainingLoanAmount * gjj_monthlyRate;
                        monthlyPrincipal = gjj_monthlyPrincipalInterests[i] - monthlyInterest;
                        gjj_remainingLoanAmount -= monthlyPrincipal;
                    }
                    sum_monthlyPrincipalInterests += gjj_monthlyPrincipalInterests[i];
                    double surplus = loanAmount + interestAmount - sum_monthlyPrincipalInterests;
                    //
                    LoanDetail detail = new LoanDetail();
                    detail.year = "第" + (i / 12 + 1) + "年";
                    detail.month = (i % 12 + 1) + "";
                    detail.payment = df.format(gjj_monthlyPrincipalInterests[i]) + "";
                    detail.payment_principal = df.format(monthlyPrincipal) + "";
                    detail.payment_interest = df.format(monthlyInterest) + "";
                    detail.surplus = df.format(surplus < 0 ? 0 : surplus) + "";
                    detail.state = (i % 12) % 2;
                    //
                    lists.add(detail);
                }
            }
        } else {
            int size = gjj_monthlyPrincipalInterests.length;
            for (int i = 0; i < size; i++) {
                if (type == 1) {
                    //等额本金
                    monthlyPrincipal = gjj_loanAmount / gjj_monthCount + sy_loanAmount / sy_monthCount;
                    monthlyInterest = gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i] - monthlyPrincipal;
                } else {
                    //等额本息
                    monthlyInterest = gjj_remainingLoanAmount * gjj_monthlyRate + sy_remainingLoanAmount * sy_monthlyRate;
                    monthlyPrincipal = gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i] - monthlyInterest;
                    gjj_remainingLoanAmount -= (gjj_monthlyPrincipalInterests[i] - gjj_remainingLoanAmount * gjj_monthlyRate);
                    sy_remainingLoanAmount -= (sy_monthlyPrincipalInterests[i] - sy_remainingLoanAmount * sy_monthlyRate);
                }
                sum_monthlyPrincipalInterests += gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i];
                double surplus = loanAmount + interestAmount - sum_monthlyPrincipalInterests;
                //
                LoanDetail detail = new LoanDetail();
                detail.year = "第" + (i / 12 + 1) + "年";
                detail.month = (i % 12 + 1) + "";
                detail.payment = df.format(gjj_monthlyPrincipalInterests[i] + sy_monthlyPrincipalInterests[i]) + "";
                detail.payment_principal = df.format(monthlyPrincipal) + "";
                detail.payment_interest = df.format(monthlyInterest) + "";
                detail.surplus = df.format(surplus) + "";
                detail.state = (i % 12) % 2;
                //
                lists.add(detail);
            }
            if (sy_monthCount - gjj_monthCount > 0) {
                int _size = sy_monthCount - gjj_monthCount + size;
                for (int i = size; i < _size; i++) {
                    if (type == 1) {
                        //等额本金
                        monthlyPrincipal = sy_loanAmount / sy_monthCount;
                        monthlyInterest = sy_monthlyPrincipalInterests[i] - monthlyPrincipal;
                    } else {
                        //等额本息
                        monthlyInterest = sy_remainingLoanAmount * sy_monthlyRate;
                        monthlyPrincipal = sy_monthlyPrincipalInterests[i] - monthlyInterest;
                        sy_remainingLoanAmount -= monthlyPrincipal;
                    }
                    sum_monthlyPrincipalInterests += sy_monthlyPrincipalInterests[i];
                    double surplus = loanAmount + interestAmount - sum_monthlyPrincipalInterests;
                    //
                    LoanDetail detail = new LoanDetail();
                    detail.year = "第" + (i / 12 + 1) + "年";
                    detail.month = (i % 12 + 1) + "";
                    detail.payment = df.format(sy_monthlyPrincipalInterests[i]) + "";
                    detail.payment_principal = df.format(monthlyPrincipal) + "";
                    detail.payment_interest = df.format(monthlyInterest) + "";
                    detail.surplus = df.format(surplus < 0 ? 0 : surplus) + "";
                    detail.state = (i % 12) % 2;
                    //
                    lists.add(detail);
                }
            }
        }

        LoanListAdapter adapter = new LoanListAdapter(mContext, lists);
        lv_loandetail.setAdapter(adapter);
    }

    @Override
    protected void onLeftButtonClick() {
        super.onLeftButtonClick();
        finish();
    }
}
